//
//  AppDelegate.m
//  bitcoin
//
//  Created by caiyajun on 2018/4/17.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "AppDelegate.h"
#import "AppLoadViewController.h"
#import "MainTabBarController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//    BaseNavigationController *login = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
//    self.window.rootViewController = login;
    
//    MainTabBarController *mainView = [[MainTabBarController alloc] init];
//    self.window.rootViewController = mainView;
    
    AppLoadViewController *loadVc = [[AppLoadViewController alloc]init];
    self.window.rootViewController = loadVc;
    
    // Override point for customization after application launch.
    
    
    // 在AppDelegate.m文件didFinishLaunchingWithOptions获取当前手机语言，并存储到NSUserDefaults。
    if (![UserDefaults objectForKey:@"appLanguage"]){
        NSArray *languages = [NSLocale preferredLanguages];
        NSString *language = [languages objectAtIndex:0];
        if ([language hasPrefix:@"zh-Hans"]) {
            //开头匹配
            [UserDefaults setObject:@"zh-Hans" forKey:@"appLanguage"];
            
        }else{
            [UserDefaults setObject:@"en" forKey:@"appLanguage"];
            
        }
        [UserDefaults synchronize];
        
    }
    
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"非活动状态");
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
     NSLog(@"进入后台");
    if(IsLogin){
        NSLog(@"%@",[self getCurrentDate]);
        [UserDefaults setObject:[self getCurrentDate] forKey:@"endAppTime"];
        [UserDefaults synchronize];
    }
}




- (NSString *)getCurrentDate {
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
//
//    [formatter setDateStyle:NSDateFormatterMediumStyle];
//
//    [formatter setTimeStyle:NSDateFormatterShortStyle];
//
//    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//
//    //设置时区,这个对于时间的处理有时很重要
//
//    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
//
//    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式

    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];


    
    
    
    
    
    return timeSp;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
    if(IsLogin){
        
        if ([UserDefaults objectForKey:@"endAppTime"]&&
            [[self getCurrentDate] integerValue] - [[UserDefaults objectForKey:@"endAppTime"] integerValue] > 599){
            [UserDefaults removeObjectForKey:K_LOGININFO_USERNAME];
            [UserDefaults removeObjectForKey:K_LOGININFO_PASSWORD];
            [UserDefaults removeObjectForKey:K_LOGININFO_TOKEN];
            [UserDefaults removeObjectForKey:K_ISEMAIL];
            [UserDefaults removeObjectForKey:K_ISGOOGLE];
            [UserDefaults removeObjectForKey:K_MOBIL];
            [UserDefaults removeObjectForKey:K_EMAIL];
            [UserDefaults synchronize];
            
            //通过window的根试图将主页展示
            UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
            MainTabBarController *mainView = [[MainTabBarController alloc] init];
            window.rootViewController = mainView;
        }
    }
}
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
